﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PizzaLunch
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        CollectionViewSource listingDataView;

        public Login()
        {
            InitializeComponent();
            listingDataView = (CollectionViewSource)(this.Resources["AccountListView"]);
            listingDataView.Source = MainControl.AllAcount;
        }

        private void btlogin_Click(object sender, RoutedEventArgs e)
        {
            TweetSource ts = MainControl.GetSource(((Button)sender).Tag as string);
            if (ts != null)
            {
                TweetAccount tac = new TweetAccount(ts);
                tac.Login();
                if (MainControl.AllAcount.Contains(tac))
                {
                    MessageBox.Show("Duplicated Account", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                MainControl.AllAcount.Add(tac);
            }
        }

        private void btClose_Click(object sender, RoutedEventArgs e)
        {
            if (MainControl.AllAcount.Count > 0)
            {
                if (MainControl.GetDefaultAccount() == null)
                {
                    MainControl.AllAcount[0].AccountCfg.isDefault = true;
                }
            }
            Util.AccountConfig.Save(MainControl.AllAcount);
            Close();
        }

        private void btDelete_Click(object sender, RoutedEventArgs e)
        {
            TweetAccount tac = lbAccount.SelectedItem as TweetAccount;
            if (tac == null)
                return;
            int Index = MainControl.AllAcount.IndexOf(tac);
            if (Index<0)
                return;
            if (MessageBox.Show("Are you sure?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                return;
            MainControl.AllAcount.RemoveAt(Index);
        }

        private void btRefresh_Click(object sender, RoutedEventArgs e)
        {
            TweetAccount tac = lbAccount.SelectedItem as TweetAccount;
            if (tac == null)
                return;
            tac.GetUserInfo();
        }

        private void btSetDefault_Click(object sender, RoutedEventArgs e)
        {
            TweetAccount tSel = lbAccount.SelectedItem as TweetAccount;
            if (tSel == null)
                return;
            TweetAccount defAcc = MainControl.GetDefaultAccount();
            if (defAcc != null)
            {
                defAcc.isDefault = false;
            }
            tSel.isDefault = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

    }
}
